<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/7/14
 * Time: 15:57
 */

namespace app\admin\service;

use app\admin\helper\LangHelper;
use app\admin\model\LogsM;

class ConsoleS
{

    /**
     * 入口
     * @param LogsM $logsM
     * @return array
     */
    public function listData(LogsM $logsM){
        $chart = input('post.chart/s');
        if ($chart == 'chart')
            return $this->chart($logsM);
        else if ($chart == 'self_chart')
            return $this->self_chart($logsM);
        return [];
    }

    /**
     * 各个type的饼图
     * @param LogsM $logsM
     * @return array
     */
    private function chart(LogsM $logsM){
        $where['op_type'] =['in',"search,export,add,edit"];
        $rs = $logsM->field('op_module,op_type,COUNT(id) as num')
            ->where($where)->group('op_module,op_type')->select()->toArray();
        $data = [];
        if(!empty($rs)){
            foreach($rs as $value){
                $data['report_'.$value['op_type']]['legend'][] =$value['op_module'];
                $data['report_'.$value['op_type']]['series'][] =['name'=>$value['op_module'],'value'=>$value['num']];
            }
            $where1['op_type'] =['in',"search,export"];
            $data['report_details'] = $this->detail_chart($logsM,$where1);
            $where2['op_type'] =['in',"login,logout"];
            $data['report_op_details'] = $this->detail_chart($logsM,$where2);
        }
        return $data;
    }

    /**
     * 总览
     * @param LogsM $logsM
     * @return array
     */
    private function detail_chart(LogsM $logsM,$where){
        $rs = $logsM->field('op_uid,op_type,COUNT(id) as num')
            ->where($where)->group('op_uid,op_type')->select()->toArray();
        $data = [];
        $legend = [];
        if(!empty($rs)){
            $pail =[];
            foreach($rs as $value){
                if(!isset($data['xAxis'])||!in_array($value['op_uid'],$data['xAxis'])){
                    $data['xAxis'][] =$value['op_uid'];
                }
                $tpye = LangHelper::convertLang('__'.$value['op_type'].'__');
                $pail[$value['op_uid']][$tpye] =$value['num'];
                $legend[$tpye] = $tpye;
            }
            $data['legend'] = array_keys($legend);
            foreach($data['xAxis'] as $user_name){
                foreach($data['legend'] as $key=>$operation_type){
                    $data['series'][$key]['name'] =$operation_type;
                    $data['series'][$key]['data'][] =isset($pail[$user_name][$operation_type])?$pail[$user_name][$operation_type]:0;
                }
            }
        }
        return $data;
    }

    /**
     * 个人详情
     * @param LogsM $logsM
     * @return array
     */
    private function self_chart(LogsM $logsM){
        $where['op_uid'] = input('post.user/s');;
        $rs=$logsM->field('op_module,op_type,COUNT(id) as num')->where($where)->group('op_module,op_type')->select()->toArray();
        $data =[];
        $legend = [];
        if(!empty($rs)){
            $pail =[];
            foreach($rs as $value){
                if(!isset($data['xAxis'])||!in_array($value['op_module'],$data['xAxis'])){
                    $data['xAxis'][] =$value['op_module'];
                }
                $tpye = LangHelper::convertLang('__'.$value['op_type'].'__');
                $pail[$value['op_module']][$tpye] =$value['num'];
                $legend[$tpye] = $tpye;
            }
            $data['legend'] = array_keys($legend);
            foreach($data['xAxis'] as $report_name){
                foreach($data['legend'] as $key=>$operation_type){
                    $data['series'][$key]['name'] =$operation_type;
                    $data['series'][$key]['data'][] =isset($pail[$report_name][$operation_type])?$pail[$report_name][$operation_type]:0;
                }
            }
        }
        return $data;
    }

}